// pages/order_list/order_list.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tab:[
           '全部订单',
            '待支付',
            '待收货',
            '已收货',
            '维权'
        ],
        tabIndex:0,
    },
    getTabIndex:function(e){
        var index=e.currentTarget.id;
        this.setData({
            tabIndex:index,
        })
    },
  // 获取formid
  getFormId: function (e) {
    var formid = e.detail.formId;
    wx.request({
      url: app.siteInfo.siteroot + 'saveformid',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        formid: formid
      },
      success: function (res) {

      }
    })
  },
  payback:function(e){
    var that = this;
    var orderid = e.currentTarget.dataset.id;
    var reason = '申请退款';
    wx.showModal({
      title: '申请退款',
      content: '确定要申请退款吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.siteInfo.shoppath + 'applypayback',
            data: {
              reason: reason,
              orderid: orderid,
              pid: app.siteInfo.pid,
              uid: app.siteInfo.uid,
              com_part_id: app.siteInfo.com_part_id
            },
            method: 'post',
            success: function (res) {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '退款已申请',
                })
                setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/order_list/order_list',
                  })
                }, 1500);
              } else {
                wx.showToast({
                  title: res.data.msg,
                  image: '/img/fail.png',
                  duration: 1500
                })
              }
            }
          })
        }
      }
    })

    
  },
  topay:function(e){
    
    var that = this;
    var orderid = e.currentTarget.dataset.id;
    wx.request({
      url: app.siteInfo.shoppath + 'payorder',
      data: {
        orderid: orderid,
        pid: app.siteInfo.pid,
        uid: app.siteInfo.uid,
        com_part_id: app.siteInfo.com_part_id
      },
      method: 'post',
      success: function (res) {
        wx.requestPayment({
          'appId': res.data.appId,
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {

          },
          'fail': function (res) {
            console.log(res);
          },
          complete: function () {
            wx.navigateTo({
              url: '/pages/order_list/order_list',
            })
          }
        })
      }
    })
  },
  cancelorder:function(e){
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '取消订单',
      content: '确定要取消订单吗？',
      success:function(res){
        if(res.confirm){
          wx.request({
            url: app.siteInfo.shoppath +'cancleorder',
            method:"post",
            data:{
              orderid:id,
              uid: app.siteInfo.uid,
              pid: app.siteInfo.pid,
              com_part_id: app.siteInfo.com_part_id,
            },
            success:function(res){
              if(res.data.code == 200){
                wx.showToast({
                  title: '订单已取消',
                })
                setTimeout(function(){
                  wx.redirectTo({
                    url: '/pages/order_list/order_list',
                  })
                },1500);
              }else{
                wx.showToast({
                  title: '取消失败',
                  image: '/img/fail.png',
                  duration: 1500
                })
              }
            }
          })
        }
      }
    })
  },
  lookwl: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/logistics_detail/index?id=' + id,
    })
  },
  queren: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认',
      content: '确认收货？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.siteInfo.shoppath + 'receiveorder',
            method: "post",
            data: {
              orderid: id,
              uid: app.siteInfo.uid,
              pid: app.siteInfo.pid,
              com_part_id: app.siteInfo.com_part_id,
            },
            success: function (res) {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '已确认收货',
                })
                setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/order_list/order_list',
                  })
                }, 1500);
              } else {
                wx.showToast({
                  title: '操作失败',
                  image: '/img/fail.png',
                  duration: 1500
                })
              }
            }
          })
        }
      }
    })
  },
  lookdetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order_detail/order_detail?id='+id,
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this;
      wx.request({
        url: app.siteInfo.shoppath+'orderlist',
        method:"post",
        data:{
          uid: app.siteInfo.uid,
          pid: app.siteInfo.pid,
          com_part_id: app.siteInfo.com_part_id,
        },
        success:function(res){
          that.setData({
            alllist: res.data.alllist,
            unpaidlist: res.data.unpaidlist,
            paidlist: res.data.paidlist,
            overlist: res.data.overlist,
            refoundlist: res.data.refoundlist,
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})